﻿using AutoMapper;
using MediatR;
using T_Rbac_APi.Application.Command.Rule;
using T_Rbac_Demain;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.RuleHandler
{
    public class RuleAddHandler : IRequestHandler<RuleAddCommand, int>
    {
        private readonly IMapper mapper;
        private readonly IRuleInformation ruleInformation;
        private readonly IDapartment dapartment;

        public RuleAddHandler(IMapper mapper, IRuleInformation ruleInformation, IDapartment dapartment)
        {
            this.mapper = mapper;
            this.ruleInformation = ruleInformation;
            this.dapartment = dapartment;
        }

        public async Task<int> Handle(RuleAddCommand request, CancellationToken cancellationToken)
        {
            request.CreateDate = DateTime.Now;
            request.CreateBy = "小明";
            request.CreateById = 0;
            var lists = await dapartment.GetAloneByid(request.Rdid);
            request.Rdname=lists.Dname;
            var list=mapper.Map<Rule>(request);
            return await ruleInformation.Add(list);
        }
    }
}
